package com.sky.controller.admin;

import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Set;

@Slf4j
@RestController
@RequestMapping("/admin/dish")
@Api(tags = "菜品管理")
public class DishController {
    @Autowired
    private DishService dishService;

    @Autowired
    private RedisTemplate redisTemplate;
    /**
     * 新增菜品
     */
    @ApiOperation("新增菜品")
    @PostMapping
    public Result addDish(@RequestBody DishDTO dishDTO){
        dishService.insert(dishDTO);
        String key="dish_"+dishDTO.getCategoryId();
//        精确清除该分类的缓存
        CleanCache(key);
        log.info("前端传来的菜品信息为:{}",dishDTO);
        return Result.success();
    }

    /**
     * 菜品分页查询
     * @param dishPageQueryDTO
     * @return
     */
    @ApiOperation("菜品分页查询")
    @GetMapping("/page")
    public Result<PageResult> SelectQuery(DishPageQueryDTO dishPageQueryDTO){
        log.info("菜品参数为:{}",dishPageQueryDTO);
        PageResult pageResult =dishService.SelectQuery(dishPageQueryDTO);
        return Result.success(pageResult);
    }

    /**
     * 删除菜品
     * @param ids
     * @return
     */
    @ApiOperation("菜品批量删除")
    @DeleteMapping
    public Result DeleteDish(@RequestParam List<Long> ids){
        log.info("删除的菜品id为:{}",ids);
//        将所有dish_开头的缓存清空
        dishService.deleteDish(ids);
        CleanCache("dish_*");
        return Result.success();

    }
    @ApiOperation("根据id查询菜品")
    @GetMapping("/{id}")
    public Result<DishVO> getById(@PathVariable Long id){
        log.info("查询菜品的id为:{}",id);
       DishVO dishVO=dishService.getById(id);
        return Result.success(dishVO);
    }

    /**
     * 修改菜品
     * @param dishDTO
     * @return
     */
    @ApiOperation("修改菜品")
    @PutMapping
    public Result  UploadDish(@RequestBody DishDTO dishDTO){
        log.info("菜品的请求数据为{}",dishDTO);
        dishService.UploadDish(dishDTO);
//        由于修改菜品涉及的分类比较复杂，所以直接清除所有dish_开头的缓存
        CleanCache("dish_*");
        return Result.success();
    }

    /**
     * 启用禁用菜品
     * @param status
     * @param id
     * @return
     */
    @ApiOperation("启用禁用菜品状态")
    @PostMapping("/status/{status}")
    public Result StatusDish(@PathVariable Integer status, Long id){
        dishService.setStatusDish(status,id);
        //清除该菜品所属分类的缓存
        DishVO dishVO=dishService.getById(id);
        String key="dish_"+dishVO.getCategoryId();
        CleanCache(key);
        log.info("菜品要启用的状态为:{}",status);
        return  Result.success();

    }

    /**
     * 根据分类id查询菜品
     * @param categoryId
     * @return
     */
    @GetMapping("/list")
    @ApiOperation("根据分类id查询菜品")
    public Result<List<Dish>> list(Long categoryId){
        List<Dish> list = dishService.list(categoryId);
        return Result.success(list);
    }

    /**
     * 清理缓存统一方法
     *
     */
    private void CleanCache(String pattern){
        Set keys = redisTemplate.keys(pattern);
        redisTemplate.delete(keys);
    }
}
